<?php

/**
 * profile actions.
 *
 * @package    namasce
 * @subpackage profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class profileActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfWebRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('profile', 'edit');
  }

 /**
  * Executes index action
  *
  * @param sfWebRequest $request A request object
  */
  public function executeEdit(sfWebRequest $request)
  {
    if (!($myProfile = Doctrine::getTable('Profile')->find(myUser::id())))
    {
      $this->redirect('/');
    }

    $this->form = new ProfileForm();

    if (!$request->isMethod('post'))
    {
      $this->form->bind($myProfile->toArray());
    }
    else
    {
      $profile = $request->getParameter('profile');
      $profile['first_name'] = trim(preg_replace('/\s{2,}/', ' ', $profile['first_name']));
      $profile['last_name'] = trim(preg_replace('/\s{2,}/', ' ', $profile['last_name']));
      $profile['user_id'] = $myProfile->user_id;
      $profile['plate'] = strtoupper(trim(preg_replace('/\s+/', '', $profile['plate'])));

      $this->form->bind($profile);
      if ($this->form->isValid())
      {
        $myProfile->synchronizeWithArray($profile);
        $myProfile->save();
        $this->redirect('@my_profile');
      }
    }
  }
}
